package com.ant.storage.service.impl;

import com.ant.storage.dto.MaterialDto;
import com.ant.storage.mapper.MaterialMapper;
import com.ant.storage.mapper.MaterialProductMapper;
import com.ant.storage.pojo.Material;
import com.ant.storage.pojo.MaterialProduct;
import com.ant.storage.service.IMaterialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-31
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Autowired
    MaterialProductMapper productMapper;

    @Override
    public List<Material> listById(Integer id) {
        List<Material> list = baseMapper.selectList(new QueryWrapper<Material>().eq("company_id", id));
        list.stream().forEach(item->{
            item.setProductList(productMapper.selectList(new QueryWrapper<MaterialProduct>().eq("parent_id",item.getId())));
        });
        return list;
    }

    @Override
    public Material saveMaterial(MaterialDto dto) {
        Material year = new Material();
        year.setIndustry(dto.getIndustry());
        year.setCompanyId(dto.getCompanyId());
        year.setAccessoryName(dto.getAccessoryName());
        year.setIndustry(dto.getIndustry());
        year.setManufacturer(dto.getManufacturer());
        year.setManufacturerTel(dto.getManufacturerTel());
        year.setAccessoryCategory(dto.getAccessoryCategory());
        year.setAccessoryNature(dto.getAccessoryNature());
        year.setAccessoryAgency(dto.getAccessoryAgency());
        year.setAccessoryKind(dto.getAccessoryKind());
        year.setAccessoryType(dto.getAccessoryType());
        year.setVocUrl(dto.getVocUrl());
        year.setCreationTime(LocalDateTime.now());
        if(dto.getId()==null){
            baseMapper.insert(year);
        }else {
            year.setId(dto.getId());
            baseMapper.updateById(year);
        }
        return year;
    }
}
